package com.lib.online.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;
import javax.validation.constraints.*;
import javax.xml.bind.annotation.*;

import org.hibernate.validator.constraints.NotEmpty;
/**
 * @author ievgen_s
 */

@XmlRootElement
@XmlType(propOrder = { "userID", 
	"userFName", 
	"userLName",
	"password",
	"books"})
@Entity
@Table(name="users")
@Cacheable
public class User implements Serializable, Comparable<User> {


    private static final long serialVersionUID = -8405939552569475337L;
    private Integer userID;
    @NotEmpty(message="{NotEmpty.user.name}")
    @Size(message="{Size.user.name}", max= 29 )
    @Pattern(regexp ="^[a-zA-Z].*" , message = "{Symbols.user.name}")
    private String userFName;
    @NotEmpty(message="{NotEmpty.user.name}")
    @Size(message="{Size.user.name}", max= 29 )
    @Pattern(regexp ="^[a-zA-Z].*" , message = "{Symbols.user.name}")
    private String userLName;
    @NotEmpty(message="{NotEmpty.user.name}")
    @Size(message="{Size.user.name}", max= 29 )
    @Pattern(regexp ="^[a-zA-Z0-9_].*" , message = "{Symbols.user.name}")
    private String password;   
    private List<Reading> books;

    public User() {
    }
    
    public User(Integer userID, 
    	    String userFName,
    	    String userLName, 
    	    String password) {
    	
    	this.userID = userID;
        this.userFName = userFName;
     	this.userLName = userLName;
    	this.password = password;
    	this.books = new ArrayList<Reading>();
    	
        }

     
    @Id 
	@Column(name="user_id")
    @GeneratedValue  
    public Integer getUserID() {
	return userID;
    }

    public void setUserID(Integer userID) {
	this.userID = userID;
    }

    @Column(name="first_name")
    public String getUserFName() {
	return userFName;
    }

    public void setUserFName(String userFName) {
	this.userFName = userFName;
    }

    @Column(name="last_name")
    public String getUserLName() {
	return userLName;
    }

    public void setUserLName(String userLName) {
	this.userLName = userLName;
    }

    @Column(name="password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @ManyToMany (targetEntity=com.lib.online.model.Reading.class,
    		cascade = {CascadeType.PERSIST, CascadeType.MERGE},
    		fetch=FetchType.EAGER)
    
    @JoinTable(name="users_books",
    joinColumns = @JoinColumn(name="user_id_fk"),
    inverseJoinColumns = @JoinColumn(name="book_id_fk")
    )   
    public List<Reading> getBooks() {
		return books;
	}

	public void setBooks(List<Reading> books) {
		this.books = books;
	}

	@Override
    public String toString() {
	return userFName + " " + userLName;
    }

    @Override
    public int compareTo(User o) {
	return this.userFName.compareTo(o.getUserFName());
    }

}
